package com.example.elderlycare.controller;

import com.example.elderlycare.controller.entity.CheckRecordRequestBody;
import com.example.elderlycare.dao.entity.Berth;
import com.example.elderlycare.dao.entity.CheckRecord;
import com.example.elderlycare.service.BerthService;
import com.example.elderlycare.service.CheckRecordService;
import com.example.elderlycare.service.entity.CheckRecordListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/check_in")
@Slf4j
public class CheckRecordController {
    @Resource
    CheckRecordService checkRecordService;

    @Resource
    BerthService berthService;

    /**
     * 查询入住登记记录
     *
     * @param elderName     老人姓名
     * @param sex           性别
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @param nurseHomeName 养老院名称
     * @param pageSize      页大小
     * @param current       当前页
     * @return 入住登记记录
     */
    @GetMapping("/query")
    public Result<CheckRecordListResponseBody> queryCheckRecords(
            @RequestParam(value = "elderName", required = false) String elderName,
            @RequestParam(value = "sex", required = false) String sex,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "nurseHomeName", required = false) String nurseHomeName,
            @RequestParam(value = "inStatusId", required = false) String inStatusId,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("elderName", elderName != null ? "%" + elderName + "%" : null);
        params.put("sex", sex);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("nurseHomeName", nurseHomeName != null ? "%" + nurseHomeName + "%" : null);
        params.put("inStatusId", inStatusId);
        try {
            CheckRecordListResponseBody res = checkRecordService.queryCheckRecord(params);
            return Result.success(res);
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 新增入住登记记录
     *
     * @param req CheckRecordRequestBody.addCheckRecord
     * @return null
     */
    @PostMapping("/add")
    public Result<?> addCheckRecord(@Valid @RequestBody CheckRecordRequestBody.AddCheckRecord req) {
        CheckRecord checkRecord = CheckRecord.builder()
                .elderId(req.getElderId())
                .startTime(new Date())
                .nurseId(req.getNurseId())
                .inStatusId(req.getInStatusId())
                .bedId(req.getBedId())
                .build();
        try {
            boolean save = checkRecordService.save(checkRecord);
            if (save) {
                Berth berth = Berth.builder()
                        .id(req.getBedId())
                        .elderId(req.getElderId())
                        .build();
                berthService.updateById(berth);
                return Result.success(null);
            } else {
                log.warn("add CheckRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }

    }

    /**
     * 修改入住登记记录
     *
     * @param req CheckRecordRequestBody.updateCheckRecord
     * @return null
     */
    @PutMapping("/update")
    public Result<?> updateCheckRecord(@Valid @RequestBody CheckRecordRequestBody.UpdateCheckRecord req) {
        CheckRecord checkRecord = CheckRecord.builder()
                .id(req.getId())
                .elderId(req.getElderId())
                .startTime(req.getStartTime())
                .nurseId(req.getNurseId())
                .inStatusId(req.getInStatusId())
                .bedId(req.getBedId())
                .build();
        try {
            boolean flag = checkRecordService.updateById(checkRecord);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("update CheckRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }

    }

    /**
     * 根据id删除入住登记记录
     *
     * @param id 入住登记id
     * @return null
     */
    @DeleteMapping("/delete")
    public Result<?> deleteCheckRecord(@RequestParam("id") @NotEmpty Integer id) {
        try {
            boolean flag = checkRecordService.removeById(id);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("delete CheckRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }

    }
}
